package com.ninong.ker.admin.service.impl;

import java.util.Arrays;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ninong.ker.admin.domain.FwGroupData;
import com.ninong.ker.admin.mapper.FwGroupDataMapper;
import com.ninong.ker.admin.service.FwGroupDataService;
import com.ninong.ker.admin.service.dto.FwGroupDataDTO;
import com.ninong.ker.common.auth.SecurityAdmin;
import com.ninong.ker.common.tools.Constants;
import com.ninong.ker.common.user.OnlineUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @author jmz
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class FwGroupDataServiceImpl extends ServiceImpl<FwGroupDataMapper, FwGroupData> implements FwGroupDataService {

	private static final String STATUS = "status";

	@Autowired
	private SecurityAdmin securityAdmin;

    @Override
	public PageInfo<FwGroupData> listPage(FwGroupDataDTO dto,String token) {
		OnlineUser user = securityAdmin.currentUser(token);
		PageHelper.startPage(dto.getCurrentPage(), dto.getPageSize());
		LambdaQueryWrapper<FwGroupData> query = new LambdaQueryWrapper<>();
		query.eq(FwGroupData::getDeleted, Constants.DEFAULT_YES);
		query.eq(FwGroupData::getCreator, user.getId());
		query.orderByDesc(FwGroupData::getCreateTime);
		if(StringUtils.isNotEmpty(dto.getGid())){
			query.eq(FwGroupData::getGid,dto.getGid());
		}
		//查询条件自行实现  
		List<FwGroupData> groupDatas = baseMapper.selectList(query);
		return new PageInfo<FwGroupData>(groupDatas);
	}

	@Override
	public void delete(String[] ids) {
		List<String> idList = Arrays.asList(ids);
		idList.forEach(id -> {		
			FwGroupData entity = new FwGroupData();
			entity.setId(id);
			entity.setDeleted(false);
			baseMapper.updateById(entity);
		});
	}

	@Override
	public void create(JSONObject dto,String token) {
		OnlineUser user = securityAdmin.currentUser(token);
		FwGroupData entity = new FwGroupData();
		entity.setCreator(user.getId());
		entity.setCreateName(user.getNickName());
		entity.setGid(dto.getString("gid"));
		entity.setStatus(dto.getBoolean("status"));
		entity.setSort(dto.getInteger("sort"));
		JSONObject form = dto.getJSONObject("form");
		entity.setValue(form.toJSONString());
		entity.setId(dto.getString("id"));
		OnlineUser onlineUser = securityAdmin.currentUser();
		entity.setTenantId(onlineUser.getTenantId());
		saveOrUpdate(entity);
	}

	@Override
	public FwGroupData get(String id) {
		FwGroupData entity = baseMapper.selectById(id);
		if (ObjectUtils.isEmpty(entity)) {
			entity = new FwGroupData();
		}
		return entity;
	}
}